package cn.com.hfuu.control;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.hfuu.EncodeImg;
import cn.com.hfuu.module.code;
import cn.com.hfuu.module.land;
import cn.com.hfuu.module.operate;
import cn.com.hfuu.module.operategz;
import cn.com.hfuu.module.operatern;
import cn.com.hfuu.module.operatesq;
import cn.com.hfuu.module.pick;
import cn.com.hfuu.module.qrcode;
import cn.com.hfuu.module.warehousetea;
import cn.com.hfuu.module.warehousetealog;
import cn.com.hfuu.module.warehouseteax;
import cn.com.hfuu.module.zl;
import cn.com.hfuu.service.CodeService;
import cn.com.hfuu.service.GzService;
import cn.com.hfuu.service.LandService;
import cn.com.hfuu.service.OperateService;
import cn.com.hfuu.service.PickService;
import cn.com.hfuu.service.QrService;
import cn.com.hfuu.service.RnService;
import cn.com.hfuu.service.SqService;
import cn.com.hfuu.service.TeaService;
import cn.com.hfuu.service.TealogService;
import cn.com.hfuu.service.TeaxService;
import cn.com.hfuu.service.UserService;
import cn.com.hfuu.service.ZlService;


@Controller
@RequestMapping("qr")
public class Qrcontrol {

	@Autowired
	QrService qrService;
	
	@Autowired
	TeaService teaService;
	
	@Autowired
	TeaxService teaxService;
	
	@Autowired
	TealogService tealogService;
	
	@Autowired
	UserService userService;
	
	@Autowired
	OperateService operateService;
	
	@Autowired
	SqService sqServicel;
	
	@Autowired
	RnService rnService;
	
	@Autowired
	GzService gzService;
	
	@Autowired
	PickService pickService;
	
	@Autowired
	LandService landService;
	
	@Autowired
	CodeService codeService;
	
	@Autowired
	ZlService zlService;
	

	/**
	 * 产生二维码
	 */
	@RequestMapping("qr")
	public void qrimg(String id,HttpServletResponse response) throws Exception{
		String url="http://192.168.43.135:8080/TeaSystem/qr/code?id="+id;
		//String url="http://192.168.0.105:8080/TeaSystem/qr/code?id="+id;
		EncodeImg handler = new EncodeImg();
		response.setContentType("image/png");
		OutputStream os = response.getOutputStream();
		BufferedImage img=handler.encode(url, "png");
		ImageIO.write(img, "png", os);
	}
	
	@RequestMapping("code")
	public ModelAndView qrtea(String id){
		ModelAndView mv=new ModelAndView();
		
		warehousetea tea=teaService.selectbyid(id);
		if(tea.getTeaZlid()!=null&&tea.getTeaZlid()!=""){
			zl zl1=zlService.selectbyid(tea.getTeaZlid());
			List<code> codelist=codeService.selectbyType("08");
			for (code code : codelist) {
				if(code.getCodeValue()==zl1.getZlResult())
					zl1.setResult(code);
			}
			mv.addObject("zl1", zl1);
		}
		
		warehousetealog tealog=tealogService.selectbyteaid(id);
		tealog.setUser(userService.selectuserbyid(tealog.getWarehouseLoguserid()));
		qrcode qr=qrService.selectbyteaid(id);
		if(qr==null){
			mv.setViewName("print/nocode");
			
			return mv;
		}
		qr.setCount(qr.getCount()+1);
		qrService.update(qr);
		operate op=operateService.selectbyid(qr.getOperateId());
		operatesq sq=sqServicel.selectbyoperateid(op.getOperateId());
		sq.setUser(userService.selectuserbyid(sq.getOperateUserid()));
		operategz gz=gzService.selectbyoperateid(op.getOperateId());
		gz.setUser(userService.selectuserbyid(gz.getOperateUserid()));
		operatern rn=rnService.selectbyoperateid(op.getOperateId());
		rn.setUser(userService.selectuserbyid(rn.getOperateUserid()));
		warehouseteax teax=teaxService.selectbyid(qr.getTeaxId());
		
		if(teax.getWarehouseZlid()!=null&&teax.getWarehouseZlid()!=""){
			zl zl2=zlService.selectbyid(teax.getWarehouseTeaxid());
			List<code> codelist=codeService.selectbyType("08");
			for (code code : codelist) {
				if(code.getCodeValue()==zl2.getZlResult())
					zl2.setResult(code);
			}
			mv.addObject("zl2", zl2);
		}
		pick pick=pickService.seletbyid(qr.getPickId());
		pick.setUser(userService.selectuserbyid(pick.getPickUserid()));
		land l=landService.selectbyid(pick.getPickLandid());
		List<code> codelist=codeService.selectbyType("06");
		for (code code : codelist) {
			if(code.getCodeValue()==pick.getPickOperate())
				pick.setCode(code);
		}
		codelist=codeService.selectbyType("11");
		for (code code : codelist) {
			if(code.getCodeValue()==l.getLandState())
				l.setCode(code);
		}
		l.setUser(userService.selectuserbyid(l.getLandFunctionary()));
		mv.addObject("qr", qr);
		mv.addObject("tea", tea);
		mv.addObject("teax", teax);
		mv.addObject("tealog", tealog);
		mv.addObject("op", op);
		mv.addObject("sq", sq);
		mv.addObject("gz", gz);
		mv.addObject("rn", rn);
		mv.addObject("pick", pick);
		mv.addObject("land", l);
		mv.setViewName("print/qrtea");
		return mv;
	}
}
